{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "from pyspark.sql import SparkSession\n",
    "from pyspark import SparkContext, SparkConf\n",
    "\n",
    "spark = SparkSession.builder.getOrCreate()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "data = [('A', 3), ('A', 4), ('A', -2),\n",
    "('B', -3), ('B', 4), ('B', 5),\n",
    "('C', 4),\n",
    "('D', -7), ('D', -9),\n",
    "('E', -5),\n",
    "('F', -2), ('F', 0)]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "rdd= spark.sparkContext.parallelize(data, 3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "count_data=rdd.mapValues(lambda x:(x,1))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[('A', (3, 1)),\n",
       " ('A', (4, 1)),\n",
       " ('A', (-2, 1)),\n",
       " ('B', (-3, 1)),\n",
       " ('B', (4, 1)),\n",
       " ('B', (5, 1)),\n",
       " ('C', (4, 1)),\n",
       " ('D', (-7, 1)),\n",
       " ('D', (-9, 1)),\n",
       " ('E', (-5, 1)),\n",
       " ('F', (-2, 1)),\n",
       " ('F', (0, 1))]"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "count_data.collect()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Mean with reducebykey"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[('B', 2.0),\n",
       " ('C', 4.0),\n",
       " ('A', 1.6666666666666667),\n",
       " ('F', -1.0),\n",
       " ('D', -8.0),\n",
       " ('E', -5.0)]"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "count_data.reduceByKey(lambda x,y:(x[0]+y[0],x[1]+y[1])).mapValues(lambda x: x[0]/x[1] ).collect()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Mean with groupbykey"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[('B', 2.0),\n",
       " ('C', 4.0),\n",
       " ('A', 1.6666666666666667),\n",
       " ('F', -1.0),\n",
       " ('D', -8.0),\n",
       " ('E', -5.0)]"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "rdd.groupByKey().mapValues(lambda x:sum(x)/len(x)).collect()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[('A', 3),\n",
       " ('A', 4),\n",
       " ('A', -2),\n",
       " ('B', -3),\n",
       " ('B', 4),\n",
       " ('B', 5),\n",
       " ('C', 4),\n",
       " ('D', -7),\n",
       " ('D', -9),\n",
       " ('E', -5),\n",
       " ('F', -2),\n",
       " ('F', 0)]"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "rdd.collect()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [],
   "source": [
    "sorted_rdd=rdd.sortBy(lambda x: x[1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[('D', -9),\n",
       " ('D', -7),\n",
       " ('E', -5),\n",
       " ('B', -3),\n",
       " ('A', -2),\n",
       " ('F', -2),\n",
       " ('F', 0),\n",
       " ('A', 3),\n",
       " ('A', 4),\n",
       " ('B', 4),\n",
       " ('C', 4),\n",
       " ('B', 5)]"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sorted_rdd.collect()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[('B', [-3, 4, 5]),\n",
       " ('C', [4]),\n",
       " ('A', [-2, 3, 4]),\n",
       " ('F', [-2, 0]),\n",
       " ('D', [-9, -7]),\n",
       " ('E', [-5])]"
      ]
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sorted_rdd.groupByKey().mapValues(lambda x:list(x)).collect()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Median with groupbykey"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[('B', 4.0), ('C', 4.0), ('A', 3.0), ('F', -1.0), ('D', -8.0), ('E', -5.0)]"
      ]
     },
     "execution_count": 41,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sorted_rdd.groupByKey().mapValues(lambda x:list(x)).\\\n",
    "mapValues(lambda x:((x[int ((len(x)-1)/2)]+x[int (len(x)/2)])/2)).collect()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Median with reducebykey"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 63,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[('B', 4.0), ('C', 4.0), ('A', 3.0), ('F', -1.0), ('D', -8.0), ('E', -5.0)]"
      ]
     },
     "execution_count": 63,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sorted_rdd.map(lambda x: (x[0], list(x[1:]))).reduceByKey(lambda x,y:x+y).\\\n",
    "mapValues(lambda x:((x[int ((len(x)-1)/2)]+x[int (len(x)/2)])/2)).collect()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
